#include "window_common.h"
#include "gkt_door.h"
#include "gkt_gpio.h"

#if defined(GKT_CONFIG_DOOR_TAMPER_ENABLE) && GKT_CONFIG_DOOR_TAMPER_ENABLE
enum {
	WIDGET_ID_TITLE = 0,
	WIDGET_ID_SELECT,
	WIDGET_ID_OK_CANCEL,
	WIDGET_ID_MAX
};

static const gkt_widget_select_attr_u sc_window_door_tamper_widget_select_attr = {
	GKT_WIDGET_SELECT_SWITCH_ATTR(GKT_WIDGET_SELECT_SWITCH_TYPE_OPEN_CLOSE, 0),
};

GKT_WINDOW_WIDGET_DESCS_DEFINE(door_tamper) = {
	[WIDGET_ID_TITLE] = GKT_WIDGET_LABEL_DESC( 
		GKT_WIDGET_F_ALIGN_MIDDLE | GKT_WIDGET_F_TITLE_MARK,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_TITLE_FG_COLOR, WINDOW_TITLE_BG_COLOR,
		0, 0, WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_door_tamper
	),
	[WIDGET_ID_SELECT] = GKT_WIDGET_SELECT_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_LARGE_SIZE,
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT, 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_EXTRA_LARGE_HEIGHT,
		&sc_window_door_tamper_widget_select_attr
	),
	[WIDGET_ID_OK_CANCEL] = GKT_WIDGET_LABEL_DESC(
		GKT_WIDGET_F_ALIGN_MIDDLE,
		WINDOW_FONT_NORMAL_SIZE, 
		WINDOW_DEFAULT_FG_COLOR, WINDOW_DEFAULT_BG_COLOR,
		0, WINDOW_WIDGET_NORMAL_HEIGHT * (WINDOW_NORMAL_ROWS - 1), 
		WINDOW_WIDGET_DEFAULT_WIDTH, WINDOW_WIDGET_NORMAL_HEIGHT,
		WINDOW_TEXT_ID_cancel_ok
	),
};

typedef struct _window_door_tamper {
	uint32_t	state;
} window_door_tamper_s;
static window_door_tamper_s s_window_door_tamper;

static void window_door_tamper_show_result(uint32_t state)
{
	window_dialog_show_big_prompt(WINDOW_DIALOG_HINT_SUCCESS,
			WINDOW_TEXT_ID_door_tamper,
			state ? WINDOW_TEXT_ID_already_opened : WINDOW_TEXT_ID_already_closed,
			WINDOW_VOICE_COMBINED_ID(set_success));
}

static int window_door_tamper_event_handler(
				uint32_t major_type, uint32_t minor_type, 
				uint32_t id, uint32_t data)
{
	int retval = GKT_EVENT_HANDLE_CONTINUE;

	switch(major_type){
		case GKT_EVENT_WINDOW:
			if (minor_type == GKT_WINDOW_INIT)
				s_window_door_tamper.state = data;
			else if (minor_type == GKT_WINDOW_ACTIVE) {
				uint32_t active_1st = data;
				if (active_1st) {
					if (s_window_door_tamper.state < WINNDOW_SWITCH_ACTION_MAX)
						window_door_tamper_show_result(s_window_door_tamper.state);
					else {
						window_common_mask_except_updn_cancel_ok();

						s_window_door_tamper.state = 
								(uint32_t)gp_window_data->door_tamper_alarm_enable;
						gkt_widget_select_set_value(WIDGET_ID_SELECT, 
								s_window_door_tamper.state);
					}
				}
				else	/* return from dialog */
					gkt_window_exit();
			}
			retval = GKT_EVENT_HANDLE_COMPLETE;
			break;
		case GKT_EVENT_WIDGET:
			if (minor_type == GKT_WIDGET_EVENT_CLICK) {
				s_window_door_tamper.state = gkt_widget_select_get_value(WIDGET_ID_SELECT);

				if(s_window_door_tamper.state != (uint32_t)gp_window_data->door_tamper_alarm_enable) {
					if (!s_window_door_tamper.state) {
						window_door_tamper_stop();
						gkt_gpio_interrupt_clear(GKT_CONFIG_DOOR_TAMPER0_GPIO_PIN);
#if defined(GKT_CONFIG_DOOR_TAMPER1_GPIO_PIN)
						gkt_gpio_interrupt_clear(GKT_CONFIG_DOOR_TAMPER1_GPIO_PIN);
#endif							
					}
					else
						gkt_door_tamper_init();
					gp_window_data->door_tamper_alarm_enable = (int)s_window_door_tamper.state;
					window_data_save();
				}

				window_door_tamper_show_result(s_window_door_tamper.state);
				retval = GKT_EVENT_HANDLE_COMPLETE;
			}
			break;					
		default:
			break;
	}

	return retval;
}

GKT_WINDOW_DESC_DEFINE(door_tamper);
#endif

